//
// LibGraphic_ncurse.cpp for nibbler in /home/moreau_r//tek2/c++/nibbler/nibbler-2016-gomes_m/lib
// 
// Made by tristan moreau
// Login   <moreau_r@epitech.net>
// 
// Started on  Sun Mar 24 10:23:08 2013 tristan moreau
// Last update Sun Mar 24 22:48:14 2013 damien lefebvre
//

#include		"IGraphicLibrary.hh"
#include		"LibGraphic_caca.hh"

LibGraphic_Caca::PictureOfElement::PictureOfElement(char c, IGraphicLibrary::e_ListOfImage type)
  : m_char(c), m_type(type)
{

}

bool		LibGraphic_Caca::PictureOfElement::isThatType(IGraphicLibrary::e_ListOfImage type) const
{
  return (m_type == type);
}

char		LibGraphic_Caca::PictureOfElement::getChar(void) const
{
  return (m_char);
}

IGraphicLibrary::e_ListOfImage	LibGraphic_Caca::PictureOfElement::getType(void) const
{
  return (m_type);
}

LibGraphic_Caca::LibGraphic_Caca(unsigned int const size_x, unsigned int const size_y, std::vector<IGraphicLibrary::InfoPicture> const & listOfElement)
{
  for (unsigned int i = 0; i < listOfElement.size(); i++)
    {
      m_pictures.push_back(PictureOfElement(listOfElement[i].getChar(), listOfElement[i].getType()));
    }
  m_cv = caca_create_canvas(size_x, size_y + 5);
  if (m_cv == NULL)
    {
      std::cout << "Failed to creat canvas" << std::endl;
    }
  else
    {
      m_dp = caca_create_display(m_cv);
      if (m_dp == NULL)
	{
	  std::cout << "Failed to open window" << std::endl;
	}
    }
}

LibGraphic_Caca::~LibGraphic_Caca(void)
{
  caca_free_display(m_dp);
  caca_free_canvas(m_cv);
}

void			LibGraphic_Caca::refreshScreen(void)
{
  caca_refresh_display(m_dp);
}

char			LibGraphic_Caca::find_carac(IGraphicLibrary::e_ListOfImage e)
{
  for (unsigned int i = 0; i < m_pictures.size(); i++)
    {
      if (m_pictures[i].getType() == e)
	return m_pictures[i].getChar();
    }
  return (-1);
}

void			LibGraphic_Caca::draw(unsigned int x, unsigned int y, IGraphicLibrary::e_ListOfImage const e, IGraphicLibrary::e_Direction const dir)
{
  char			carac;

  carac = find_carac(e);
  if (carac == -1)
    return ;
  if (e == IMG_SNAKEHEAD || e == IMG_SNAKEFOOT || e == IMG_SNAKE_PART1 || e == IMG_SNAKE_PART2)
    caca_set_color_ansi(m_cv, CACA_GREEN, CACA_BLACK);
  else if (e == IMG_BONUS5PART_1)
    caca_set_color_ansi(m_cv, CACA_BLUE, CACA_BLACK);
  else if (e == IMG_BONUS5PART_2)
    caca_set_color_ansi(m_cv, CACA_RED, CACA_BLACK);
  else if (e == IMG_BONUSPART)
    caca_set_color_ansi(m_cv, CACA_CYAN, CACA_BLACK);
  else
    caca_set_color_ansi(m_cv, CACA_WHITE, CACA_BLACK);
  caca_printf(m_cv, x, y, "%c", carac);
}

IGraphicLibrary::e_TypeOfEvent	LibGraphic_Caca::checkEvent(void)
{
    caca_event_t		ev;

  while (caca_get_event(m_dp, CACA_EVENT_ANY, &ev, 0))
    {
      if (caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
	{
	  switch(caca_get_event_key_ch(&ev))
	    {
	    case 'q':
	    case CACA_KEY_ESCAPE:
	      return (IGraphicLibrary::EXIT);
	    case 'p':
	      return (IGraphicLibrary::PAUSE);
	    case CACA_KEY_UP:
	      return (IGraphicLibrary::KB_UP);
	    case CACA_KEY_DOWN:
	      return (IGraphicLibrary::KB_DOWN);
	    case CACA_KEY_LEFT:
	      return (IGraphicLibrary::KB_LEFT);
	    case CACA_KEY_RIGHT:
	      return (IGraphicLibrary::KB_RIGHT);
	    }
	}
      else if (caca_get_event_type(&ev) & CACA_EVENT_QUIT)
	return (IGraphicLibrary::EXIT);
    }
  return (IGraphicLibrary::NONE);
}
     
void			LibGraphic_Caca::draw_footer(unsigned int x, unsigned int y, std::string const & str)
{
  caca_set_color_ansi(m_cv, CACA_WHITE, CACA_BLACK);

  caca_printf(m_cv, x, y, "%s", str.c_str());  
}

extern "C"
{
  IGraphicLibrary	*make_IGraphicLibrary(unsigned int size_x, unsigned int size_y, std::vector<IGraphicLibrary::InfoPicture> const & list)
  {
    return (new LibGraphic_Caca(size_x, size_y, list));
  }
}
